//
//  MenuModelH.swift
//  ShiZhe
//
//  Created by 陈卿伟 on 16/9/26.
//  Copyright © 2016年 陈卿伟. All rights reserved.
//

import UIKit
import SwiftyJSON

class MenuModelH: NSObject {
    var menu:[JSON]
    var selecte1:Int
    
    var count1 = 0//第一级的数量
    var count2 = 0
    var count3 = 0
    var menuID1:Array<String> = []//记录第一级的menu的id
    var menuID2:Array<String> = []
    var menuID3:Array<Array<String>> = [[]]
    
    
    var menuName1:Array<String> = []//记录第一级的menuName
    var menuName2:Array<String> = []
    var menuName3:Array<Array<String>> = [[]]
    init(menu:[JSON],selecte1:Int){
        self.menu = menu
        self.selecte1 = selecte1
    }
    
    //清空数据 只留一次
    func deleteMenu(){
        count1 = 0//第一级的数量
        count2 = 0
        count3 = 0
        menuID1 = ["","","","","","","","","",""]//记录第一级的menu的id
        menuID2 = []
        menuID3 = [[],[],[],[],[],[],[],[],[],[],[]]
        
        menuName1 = []//记录第一级的menuName
        menuName2 = []
        menuName3 = [[],[],[],[],[],[],[],[],[],[],[]]
    }
    
    
    //计算每一级的item数量
    func getMenuItemCount(){
        for i in 0..<menu.count{
            let parent_id = menu[i]["parent_id"].stringValue
            if parent_id == "0"{
                menuID1.insert(menu[i]["menu_id"].stringValue, atIndex: count1)
                menuName1.insert(menu[i]["menu_name"].stringValue, atIndex: count1)
                count1 += 1
            }else if parent_id == menuID1[selecte1-1]{
                menuID2.insert(menu[i]["menu_id"].stringValue, atIndex: count2)
                menuName2.insert(menu[i]["menu_name"].stringValue, atIndex: count2)
                count2 += 1
            }else{
                for z in 0..<menuID2.count{
                    if parent_id == menuID2[z]{
                        if menuName3[z].isEmpty == true{
                            menuName3.insert([menu[i]["menu_name"].stringValue], atIndex: z)
                            menuID3.insert([menu[i]["menu_id"].stringValue], atIndex: z)
                        }else{
                            menuName3[z].insert(menu[i]["menu_name"].stringValue, atIndex:  menuName3[z].count)
                            menuID3[z].insert(menu[i]["menu_id"].stringValue, atIndex:  menuID3[z].count)
                        }
                    }
                }
            }
        }
    }

}
